package edu.bu.cs673.superkids123.children.view.games;

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractView;
import edu.bu.cs673.superkids123.children.controller.games.GameResultsController;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.view.components.AchievementsEarnedPanel;
import edu.bu.cs673.superkids123.common.model.Achievement;
import edu.bu.cs673.superkids123.common.model.Child;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * NumberMatchingGameController
 * @author Harris Tracy
 *
 */

public class GameResultsView extends AbstractView<JPanel> 
implements ActionListener
{
	private JLabel encouragementField;
	private JLabel childScoreField;
	private JButton returnToMenuButton; 
	private ImageIcon encouragementImage;
    private int score;
	private Child child;
	private GameResultsController controller;
	
	public GameResultsView(AbstractFrame mainFrame) {
		super(mainFrame);
		controller = getMainFrame().getController(GameResultsController.class);
	}
	/**
	 * @wbp.parser.entryPoint
	 */

	@Override
	protected JPanel layout() {
		JPanel p = new JPanel();
		p.setSize(800, 600);
		p.setBackground(Color.WHITE);
		GridBagLayout gbl_p = new GridBagLayout();
		gbl_p.columnWidths = new int[]{100, 537, 116};
		gbl_p.rowHeights = new int[]{70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		p.setLayout(gbl_p);

		child = (Child) authController.getCurrentUser();

		// @author Joshua Stein
		if (controller.inArithmaticGame || controller.inMatchGame ||
		    controller.inArcheryGame || controller.inNumberMatchingGame) {
			score = controller.correct*10;
		}
		else if (controller.inSubtractionGame) {
			score = controller.correct*20;  // only 5 questions
		}
		// END @author Joshua Stein
		ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
		
		//encouragementField to display encouragement, different message & picture based on child's score
		if (score == 100) {
			{encouragementField = new JLabel("Perfect Score,".concat(child.getName().concat("!!")), JLabel.CENTER);
			encouragementImage = provider.getImage(ChildrenResource.imageEncouragementSmile);
			}
        } 
		else if (score >= 90) {
        	{encouragementField = new JLabel("Excellect,".concat(child.getName().concat("!!")), JLabel.CENTER);
        	encouragementImage = provider.getImage(ChildrenResource.imageEncouragementSun);
        	}
        } else if (score >= 80) {
        	{encouragementField = new JLabel("Great Job,".concat(child.getName().concat("!!")), JLabel.CENTER);
        	encouragementImage = provider.getImage(ChildrenResource.imageEncouragementStar);
        	}
        } else if (score >= 70) {
        	{encouragementField = new JLabel("Good Job,".concat(child.getName().concat("!")), JLabel.CENTER);
        	encouragementImage = provider.getImage(ChildrenResource.imageEncouragementBeaver );
        	}
        } else if (score >= 60) {
        	{encouragementField = new JLabel("Nice Work,".concat(child.getName().concat("!")), JLabel.CENTER);
        	encouragementImage = provider.getImage(ChildrenResource.imageEncouragementFish );
        	}
        } else {
        	{encouragementField = new JLabel("Keep Trying,".concat(child.getName().concat("!")), JLabel.CENTER);
        	encouragementImage = provider.getImage(ChildrenResource.imageEncouragementWorm);
        	}
        }
	
		encouragementField.setBackground(Color.WHITE);
		encouragementField.setFont(new Font("Arial", Font.PLAIN, 56));
		GridBagConstraints encouragementConstraints = new GridBagConstraints();
		encouragementConstraints.insets = new Insets(0, 0, 5, 5);
		encouragementConstraints.gridx = 1;
		encouragementConstraints.gridy = 0;
		p.add(encouragementField, encouragementConstraints);
	
		JPanel pic = new JPanel();  
		pic.setBorder(null);
		pic.setBackground(Color.WHITE);
		pic.setSize(100,100);  
		  
		GridBagConstraints gbc_pic = new GridBagConstraints();
		gbc_pic.insets = new Insets(0, 0, 5, 5);
		gbc_pic.gridx = 0;
		gbc_pic.gridy = 0;
		p.add(pic, gbc_pic);   
		
		JLabel lab = new JLabel(encouragementImage, SwingConstants.CENTER);
		GridBagConstraints gbc_lab = new GridBagConstraints();
		gbc_lab.fill = GridBagConstraints.BOTH;
		gbc_lab.insets = new Insets(0, 0, 5, 5);
		gbc_lab.gridx = 1;
		gbc_lab.gridy = 3;
		p.add(lab, gbc_lab);
		
		//childScoreField to display child's score
		childScoreField = new JLabel(String.valueOf(score), JLabel.CENTER);
		childScoreField.setBackground(Color.WHITE);
		childScoreField.setFont(new Font("Arial", Font.PLAIN, 56));
		GridBagConstraints childScoreConstraints = new GridBagConstraints();
		childScoreConstraints.insets = new Insets(0, 0, 5, 5);
		childScoreConstraints.gridx = 1;
		childScoreConstraints.gridy = 4;
		p.add(childScoreField, childScoreConstraints);
		
		//returnToMenuButton for navigation to activities menu  
		returnToMenuButton = new JButton ("Return to the Main Menu"); 
		GridBagConstraints returnToMenuConstraints = new GridBagConstraints();
		returnToMenuConstraints.insets = new Insets(0, 0, 5, 5);
		returnToMenuConstraints.gridx = 1;
		returnToMenuConstraints.gridy = 6;
		p.add(returnToMenuButton,returnToMenuConstraints);
	
		returnToMenuButton.addActionListener(this);
		
		// Achievement panel
		List<Achievement> earned = getMainFrame().getController(GameResultsController.class)
				.getEarnedAchievements();
		if (earned != null && earned.size() > 0) {
			AchievementsEarnedPanel earnedPanel = new AchievementsEarnedPanel(earned);
			GridBagConstraints gbc_earned = new GridBagConstraints();
			gbc_earned.fill = GridBagConstraints.BOTH;
			gbc_earned.gridx = 1;
			gbc_earned.gridy = 7;
			p.add(earnedPanel, gbc_earned);
		}
		
		
		return p;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			if (e.getSource() == returnToMenuButton) {
				controller
				.returnToMenuButtonPressed();
				} 
			}
		}
	}


	